import {
  getList,
  delList,
  deviceOpenOrStop,
  delFalut,
  getDetail,
  addDevice,
  editDevice,
  devideNoCheck,
} from 'service/actions/deviceLedger';
import { message } from 'antd';
import router from 'umi/router';

export default {
  namespace: 'deviceLedger',

  state: {
    list: [],
    detail: {},
    total: 0,
    pageCount: 1,
    allPages: 1,
    notNo: false, // 是否有重复编码
    details: {},
    serachParams: {}
  },

  effects: {
    // 获取列表数据
    *getList({ payload }, { put, call }) {
      const data = yield call(getList, payload || {});
      if (data) {
        if (data.code === '000000') {
          yield put({
            type: 'updateState',
            payload: {
              list: data.data.records,
              total: data.data.total,
              pageCount: data.data.current,
              allPages: data.data.pages,
            },
          });
        }
      }
    },
    // 新增数据
    *addDevice({ payload }, { put, call }) {
      const data = yield call(addDevice, payload);
      if (data.code === '000000') {
        message.success('操作成功！')
        router.push('/deviceLedger')
      } else {
        message.error(data.mesg);
      }
      return data;
    },
    // 编辑数据
    *editDevice({ payload }, { put, call }) {
      const data = yield call(editDevice, payload);
      if (data.code === '000000') {
        message.success('操作成功！')
        router.push('/deviceLedger')
      } else {
        router.push('/deviceLedger')
        message.error(data.mesg);
      }
      return data;
    },
    // 删除列表数据
    *delList({ payload }, { put, call }) {
      const data = yield call(delList, payload);
      if (data.code === '000000') {
        message.success('删除成功！');
        yield put({
          type: 'updateSerachParams',
          payload: {
            serachParams: {
              current: 1
            }
          }
        });
      } else {
        message.error(data.mesg);
      }
      return data;
    },
    // 获取详情数据
    *getDetail({ payload }, { put, call }) {
      try {
        const data = yield call(getDetail, payload);
        if (data.code === '000000') {
          yield put({
            type: 'updateDetail',
            payload: { detail: data.data },
          });
        } else {
          router.push('/deviceLedger')
          message.error(data.mesg)
        }
      } catch (error) {
        console.log('error', error)
      }
    },
    // 获取详情页的详情数据
    *getDetails({ payload }, { put, call }) {
      try {
        const data = yield call(getDetail, payload);
        if (data.code === '000000') {
          yield put({
            type: 'updateDetails',
            payload: { details: data.data },
          });
        } else {
          router.push('/deviceLedger')
          message.error(data.mesg)
        }
      } catch (error) {
        console.log('error', error)
      }
    },
    // 设备编号查重
    *deviceNoCheck({ payload }, { put, call }) {
      const data = yield call(devideNoCheck, payload);
      if (data.code === '000000') {
        yield put({
          type: 'updateHasNo',
          payload: { notNo: data.data },
        });
      }
    },
    // 编辑是判断编号去重
    *changeNoCheck({ payload }, { put }) {
      yield put({
        type: 'updateHasNo',
        payload: { notNo: true },
      });
    },
    // 设备停用启用
    *deviceStopOrOpen({ payload }, { put, call }) {
      const { id, ledgerStatus, pageNations } = payload
      const data = yield call(deviceOpenOrStop, { id, ledgerStatus });
      if (data.code === '000000') {
        message.success('操作成功！');
        yield put({
          type: 'updateSerachParams',
          payload: {
            serachParams: {
              current: 1
            }
          }
        });
      } else {
        message.error(data.mesg);
        yield put({
          type: 'updateSerachParams',
          payload: {
            serachParams: {
              current: 1
            }
          }
        });
      }
    },
    // 故障处理
    *delFalut({ payload }, { put, call }) {
      const { contnet, pageNations } = payload
      const data = yield call(delFalut, { ...contnet });
      if (data.code === '000000') {
        message.success('操作成功！');
        yield put({
          type: 'updateSerachParams',
          payload: {
            serachParams: {
              current: 1
            }
          }
        });
      } else {
        message.error(data.mesg);
        yield put({
          type: 'updateSerachParams',
          payload: {
            serachParams: {
              current: 1
            }
          }
        });
      }
    },
  },

  reducers: {
    // 更新List
    updateState(state, { payload: { list, total, pageCount, allPages } }) {
      return { ...state, list, total, pageCount, allPages };
    },
    // 更新Detail
    updateDetail(state, { payload: { detail } }) {
      return { ...state, detail };
    },
    // 更新hasNo
    updateHasNo(state, { payload: { notNo } }) {
      return { ...state, notNo };
    },
    // 更新Detail
    updateDetails(state, { payload: { details } }) {
      return { ...state, details };
    },
    // 更细查询参数
    updateSerachParams(state, { payload: { serachParams } }) {
      return { ...state, serachParams }
    }
  },
};
